package com.cqs.leetcode;


import java.util.Stack;

/**
 * Created by cqs on 2018/3/29.
 */
public class BaseballGame {

    public int calPoints(String[] ops) {
        Stack<Integer> stack = new Stack<>();
        int sum = 0;
        for (String op : ops) {
            int num = 0;
            if ("+".equals(op)) {
                Integer pop = stack.pop();
                num = pop + stack.peek();
                stack.push(pop);
                stack.push(num);
            } else if ("D".equals(op)) {
                Integer pop = stack.peek();
                num = pop << 1;
                stack.push(num);
            } else if ("C".equals(op)) {
                num = -stack.pop();
            } else {
                num = Integer.valueOf(op);
                stack.push(num);
            }
            sum += num;
//            System.out.println("操作数:" + op + "\tnum:" + num + "\tsum = " + sum);
        }
        return sum;
    }

    public static void main(String[] args) {
//        String[] data = {"5", "2", "C", "D", "+"};
        String[] data = {"5","-2","4","C","D","9","+","+"};
        BaseballGame games = new BaseballGame();
        int result = games.calPoints(data);
        System.out.println(result);
    }
}
